<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\DB;

class Topic extends Model
{
    use Notifiable;
    use SoftDeletes;

    protected $dates = ['deleted_at'];
    protected $table = 'topics';

    // 列表
    public static function getList($request)
    {
        $query = self::select('id', 'title', 'en_title', 'img', 'description', 'cateid', 'catename', 'status', 'created_at', 'updated_at');
        if (isset($request->status) && $request->status) {
            $query->where('status', $request->status);
        }
        if (isset($request->cateid) && $request->cateid) {
            $query->where('cateid', $request->cateid);
        }
        if (isset($request->keywords) && $request->keywords) {
            $query->where('title', 'like', '%' . $request->keywords . '%');
        }
        if (isset($request->sort) && $request->sort) {
            $query->orderBy($request->sort, $request->order);
        } else {
            $query->orderBy('id', 'desc');
        }
        $res = $query->paginate($request->size);
        return $res ? $res->toArray() : [];
    }

    public static function getDetail($id) {
        $res = self::select('*')->where('id', $id)->first();
        return $res ? $res->toArray() : [];
    }

    // 详情
    public static function getInfo($id)
    {
        $res = self::select('id', 'title', 'description', 'url', 'content', 'en_content')->where('cateid', $id)->orderBy('updated_at', 'desc')->first();
        return $res ? $res->toArray() : [];
    }

    // 删除
    public static function del($id)
    {
        $res = static::where('id', $id)->delete();
        return $res;
    }
}
